package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class SearchController {

    @Resource
    private SearchService searchService;
    /**
     * 初始化索引库
     */
    @GetMapping("initialization")
    public ResponseEntity<String> init() {
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    /**
     * 查询自动补全数据
     * @param key 用户输入的关键字
     * @return 补全的内容列表
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key){
        return searchService.getSuggestion(key);
    }

    /**
     * 分页搜索商品数据
     * @param searchParam 请求参数
     * @return 分页结果
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO searchParam){
        return searchService.searchGoods(searchParam);
    }

    /**
     * 聚合查询分类 品牌 信息
     * @param searchParam 请求参数
     * @return 分页结果
     */
    @PostMapping("/filter")
    public Mono<Map<String,List<?>>> listFilter(@RequestBody SearchParamDTO searchParam){
        return searchService.listFilter(searchParam);
    }

}
